<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	function xStatusAktif($status) {
		return $status?'<span class="span_blue">Aktif</span>':'<span class="span_red">Tidak Aktif</span>';
	}

	function xRemoveSpecial($mystr,$stripapp=true) {
		// $pattern = '/[%&;()\"';
		$pattern = '/[%&;\"';
		if($stripapp === false) // tidak stripping ', tapi direplace jadi '', biasanya dipakai di nama
			$mystr = str_replace("'","''",$mystr);
		else
			$pattern .= "\'";
		if(preg_match('/[A-Za-z%\/]/',$mystr)) // kalau ada alfabet, %, atau /, strip <>
			$pattern .= '<>';
		$pattern .= ']|--/';
		
		return preg_replace($pattern, '$1', $mystr);
	}

	// mengubah format tanggal dari dd-mm-yyyy menjadi yyyy-mm-dd dan sebaliknya
	function xFormatDate($dmy,$delim='-',$todelim='-') {
		if($dmy == '')
			return '';
		if($dmy == 'null')
			return 'null';
		list($y,$m,$d) = explode($delim,substr($dmy,0,10));
		return $d.$todelim.$m.$todelim.$y;
	}
	
	function xFormatDate2($dmy,$delim='/',$todelim='-') {
		if($dmy == '')
			return '';
		if($dmy == 'null')
			return 'null';
		list($y,$m,$d) = explode($delim,substr($dmy,0,10));
		return $d.$todelim.$m.$todelim.$y;
	}
	
	// mengubah format tanggal dari yyyy-mm-dd menjadi format indonesia
	function xFormatDateInd($ymd,$full=true,$dmy=false,$delim='-',$withtime=false) {
		if ($withtime)
			$time = substr($ymd,11,5);

		if($ymd == '')
			return '';
		if($ymd == 'null')
			return 'null';
		
		if($dmy)
			list($d,$m,$y) = explode($delim,substr($ymd,0,10));
		else
			list($y,$m,$d) = explode($delim,substr($ymd,0,10));
		
		return (int)$d.' '.xIndoMonth($m,$full).' '.$y. ' ' . $time;
	}
	
	// mengubah format tanggal, tapi ada time dibelakangnya
	function xFormatDateTime($dmy,$delim='-') {
		if($dmy == '')
			return '';
		if($dmy == 'null')
			return 'null';
		return xFormatDate(substr($dmy,0,10)).substr($dmy,10,6);
	}
	
	// mengubah format hhii menjadi hh:ii
	function xFormatTime($hi,$delim=':') {
		if($hi == '')
			return '';
		
		$hi = str_pad($hi,2,'0',STR_PAD_LEFT);
		return substr($hi,0,2).':'.substr($hi,2,2);
	}

	// nama hari di bahasa indonesia
	function xIndoDay($nhari,$full=true) {
		if($full) {
			switch($nhari) {
				case 0: return "Minggu";
				case 1: return "Senin";
				case 2: return "Selasa";
				case 3: return "Rabu";
				case 4: return "Kamis";
				case 5: return "Jumat";
				case 6: return "Sabtu";
			}
		}
		else {
			switch($nhari) {
				case 0: return "Min";
				case 1: return "Sen";
				case 2: return "Sel";
				case 3: return "Rab";
				case 4: return "Kam";
				case 5: return "Jum";
				case 6: return "Sab";
			}
		}
	}
	
	// nama bulan di bahasa indonesia
	function xIndoMonth($nbulan,$full=true) {
		if($full) {
			switch($nbulan) {
				case 1: return "Januari";
				case 2: return "Februari";
				case 3: return "Maret";
				case 4: return "April";
				case 5: return "Mei";
				case 6: return "Juni";
				case 7: return "Juli";
				case 8: return "Agustus";
				case 9: return "September";
				case 10: return "Oktober";
				case 11: return "Nopember";
				case 12: return "Desember";
			}
		}
		else {
			switch($nbulan) {
				case 1: return "Jan";
				case 2: return "Feb";
				case 3: return "Mar";
				case 4: return "Apr";
				case 5: return "Mei";
				case 6: return "Jun";
				case 7: return "Jul";
				case 8: return "Agu";
				case 9: return "Sep";
				case 10: return "Okt";
				case 11: return "Nop";
				case 12: return "Des";
			}
		}
	}
	
	function xGetDayIndex($str) {
		if ($str == 'monday')
			return '1';
		elseif ($str == 'tuesday')
			return '2';
		elseif ($str == 'wednesday')
			return '3';
		elseif ($str == 'thursday')
			return '4';
		elseif ($str == 'friday')
			return '5';
		elseif ($str == 'saturday')
			return '6';
		elseif ($str == 'sunday')
			return '7';

		return false;
	}

	function xGetDayString($idx) {
		if ($idx == '1')
			return 'monday';
		elseif ($idx == '2')
			return 'tuesday';
		elseif ($idx == '3')
			return 'wednesday';
		elseif ($idx == '4')
			return 'thursday';
		elseif ($idx == '5')
			return 'friday';
		elseif ($idx == '6')
			return 'saturday';
		elseif ($idx == '7')
			return 'sunday';

		return false;
	}

	function xFormatNumber($num) {
		return number_format($num,0,',',',');
	}
	
	function xStripNumber($number, $str=',') {
		return str_replace($str,'',$number);
	}

	function xNumberToWords($x, $lang='id')	{
		if ($lang=='id') {
			$abil = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
			if ($x < 12)
			  return " " . $abil[$x];
			elseif ($x < 20)
			  return xNumberToWords($x - 10, $lang) . "belas";
			elseif ($x < 100)
			  return xNumberToWords($x / 10, $lang) . " puluh" . xNumberToWords($x % 10, $lang);
			elseif ($x < 200)
			  return " seratus" . xNumberToWords($x - 100);
			elseif ($x < 1000)
			  return xNumberToWords($x / 100, $lang) . " ratus" . xNumberToWords($x % 100, $lang);
			elseif ($x < 2000)
			  return " seribu" . xNumberToWords($x - 1000, $lang);
			elseif ($x < 1000000)
			  return xNumberToWords($x / 1000, $lang) . " ribu" . xNumberToWords($x % 1000, $lang);
			elseif ($x < 1000000000)
			  return xNumberToWords($x / 1000000, $lang) . " juta" . xNumberToWords($x % 1000000, $lang);
		}
	}
	function spellNumC($num) {
		$a_num = explode('.',$num);
		$n = count($a_num);
		$spell='';
		for($i=0;$i<$n;$i++) {
			$bag = $a_num[$i];
			if($a_num[$i]!='00'){
				$spell .= xNumberToWords($bag);
			}else{
				$spell .= ' nol nol';
			}
			
			if($i < ($n-1)){
				$spell .= ' koma ';
			}
		}	
		return $spell;
	}
	
	function xSetList($sql) {
		$ci = &get_instance();
		$list = array();
		$rs = xGetRows($sql);
		foreach ($rs as $row) {
			$list[$row['id']] = $row['val'];
		}
		return $list;
	}
	
	function xIsInRangeTime($start, $end, $now=null) {
		if (!$now) {
			$now = date('Y-m-d');
		}
		
		if ($start > $now || $end < $now)
			return false;
		
		return true;
	}
	
	function xIsValidDate($str) {
		if (preg_match('/-/',$str)) {
			$date = explode("-", $str);
			$month = (int)$date[1];
			$day = (int)$date[2];
			$year = (int)$date[0];
			return checkdate($month,$day,$year);
		}
		return false;
	}
	
	function xGetDiffDate($date1, $date2) {
		$diff = abs($date2 - $date1);
		return floor($diff / (60*60*24));
	}
	
	function xPageTitle($page_title=null) {
		$ret = '';
		if ($page_title){
			$ret = "<h1 style=\"border-bottom:1px dashed #ccc;padding-bottom:7px;margin-bottom:5px\">{$page_title}</h1>";
		}
		return $ret;
	}

    function xPadNoAgenda($noagenda) {
		return str_pad((int)$noagenda,4,'0',STR_PAD_LEFT);
	}

    function xPadNoRegistrasi($noreg) {
		return str_pad((int)$noreg,5,'0',STR_PAD_LEFT);
	}

	function xRomanNumerals($num) {
		$n = intval($num);
		$res = '';

		/*** roman_numerals array  ***/
		$roman_numerals = array(
					'M'  => 1000,
					'CM' => 900,
					'D'  => 500,
					'CD' => 400,
					'C'  => 100,
					'XC' => 90,
					'L'  => 50,
					'XL' => 40,
					'X'  => 10,
					'IX' => 9,
					'V'  => 5,
					'IV' => 4,
					'I'  => 1);
	 
		foreach ($roman_numerals as $roman => $number) {
			/*** divide to get  matches ***/
			$matches = intval($n / $number);
	 
			/*** assign the roman char * $matches ***/
			$res .= str_repeat($roman, $matches);
	 
			/*** substract from the number ***/
			$n = $n % $number;
		}
	 
		/*** return the res ***/
		return $res;
    }
	
	function hitungTelat($tenggat, $hari_ini){
		$hitungdenda = (strtotime($hari_ini)-strtotime($tenggat))/86400;
		if($hitungdenda>0)
			return $hitungdenda;
		else
			return 0;
	}
